package com.hunan.api.catv.service;

import com.hunan.api.catv.entity.CodeResult;
import com.hunan.api.catv.entity.ServiceResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class ResultService {

    protected Logger log = LoggerFactory.getLogger(getClass());

    public ServiceResult buildOK(Object data, String msg) {
        ServiceResult serviceResult = new ServiceResult();
        serviceResult.setData(data);
        serviceResult.setMsg(msg);
        serviceResult.setStatus(true);
        return serviceResult;
    }

    public ServiceResult buildFail(Object data, String msg) {
        ServiceResult serviceResult = new ServiceResult();
        serviceResult.setData(data);
        serviceResult.setMsg(msg);
        serviceResult.setStatus(false);
        return serviceResult;
    }

    public ServiceResult buildOK(Object data) {
        ServiceResult serviceResult = new ServiceResult();
        serviceResult.setData(data);
        serviceResult.setMsg("Success");
        serviceResult.setStatus(true);
        return serviceResult;
    }


    public ServiceResult buildFail(String msg) {
        ServiceResult serviceResult = new ServiceResult();
        serviceResult.setData("");
        serviceResult.setMsg(msg);
        serviceResult.setStatus(false);
        return serviceResult;
    }

    public CodeResult buildFailCode(String code, String msg) {
        CodeResult codeResult = new CodeResult();
        codeResult.setData("");
        codeResult.setMsg(msg);
        codeResult.setCode(code);
        return codeResult;
    }

    public CodeResult buildOKCode() {
        CodeResult codeResult = new CodeResult();
        codeResult.setData("");
        codeResult.setMsg("校验成功");
        codeResult.setCode("0000");
        return codeResult;
    }

    public ServiceResult build(Object data, String msg, Boolean status) {
        ServiceResult serviceResult = new ServiceResult();
        serviceResult.setData(data);
        serviceResult.setMsg(msg);
        serviceResult.setStatus(status);
        return serviceResult;
    }

    public boolean isEmpty(Object... parameters) {
        for (Object parameter : parameters) {
            if (ObjectUtils.isEmpty(parameter)) {
                return true;
            }
        }
        return false;
    }

    public HttpServletRequest getServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
}
